<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
/*
   首页公告
*/
class NoticeCenterController extends Controller{
    /*
        公告展示
    */
    public function ShowNotice($request){
            if(!CheckController::CheckUser($request)){
                return RequestBackController::Structure(205);
            }
            $pos=$request->all();
            $notice = DB::connection('mysql_other')->table('yl_notice as n')->select('n.notice_id as NoticeId','n.notice_title as NoticeTitle','n.notice_content as NoticeContent','n.notice_creater_id as NoticeCreaterId','n.notice_created_time as NoticeCreatedTime','n.notice_updater_id as NoticeUpdaterId','n.notice_update_time as NoticeUpdateTime')
                                                ->get()
                                                ->toArray();

            if($notice){
                return RequestBackController::Structure(501,$notice);
            }else{
                return RequestBackController::Structure(504);
            }


        }


    /*
        公告详情
    */

    public function detailsNotice($request){
        if(!CheckController::CheckUser($request)){
                return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['NoticeId'])){//
           return RequestBackController::Structure(502);
        }else{
            $info = DB::connection('mysql_other')->table('yl_notice as n')->select('n.notice_id as NoticeId','n.notice_title as NoticeTitle','n.notice_content as NoticeContent','n.notice_creater_id as NoticeCreaterId','n.notice_created_time as NoticeCreatedTime','n.notice_updater_id as NoticeUpdaterId','n.notice_update_time as NoticeUpdateTime','u.user_id as UserId','u.user_name as UserName')
                                                ->leftJoin("yl_user as u",function($join){
                                                    $join->on('u.user_id','=','n.notice_updater_id');
                                                })
                                                ->where('n.notice_id',$pos['NoticeId'])
                                                ->get()
                                                ->map(function ($val){
                                                    $val = (array)$val;
                                                    return $val;
                                                })
                                                ->toArray();
            foreach ($info as $k => $v) {
                $info[$k]['NoticeCreatedTime'] = date('Y-m-d',$v['NoticeCreatedTime']);
                $info[$k]['NoticeUpdateTime'] = date('Y-m-d',$v['NoticeUpdateTime']);
            }
            if($info){
                return RequestBackController::Structure(501,$info);

            }else{
                return RequestBackController::Structure(504);   

            }

        }
        

    }

    /*
        编辑公告
    */
    public function EditNotice($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['NoticeId','UserId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){//超级管理员
                    $map =array();
                    $map['notice_title'] =$pos['NoticeTitle'];
                    $map['notice_content'] =$pos['NoticeContent'];
                    $map['notice_updater_id'] = $pos['UserId'];
                    $map['notice_update_time'] = time();
                    $UpNotice = DB::connection('mysql_other')->table('yl_notice')->where('notice_id',$pos['NoticeId'])->update($map);
                    if($UpNotice){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
              
            }else{
                return RequestBackController::Structure(403);
            }

        }

    }


    /*
    添加公告
    */
    public function AddNotice($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){//超级管理员
                    $map =array();
                    $map['notice_title'] =$pos['NoticeTitle'];
                    $map['notice_content'] =$pos['NoticeContent'];
                    $map['notice_creater_id'] = $pos['UserId'];
                    $map['notice_created_time'] = time();
                    $map['notice_updater_id'] = $pos['UserId'];
                    $map['notice_update_time'] = time();
                    $AddNotice = DB::connection('mysql_other')->table('yl_notice')->insert($map);
                    if($AddNotice){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
              
            }else{
                return RequestBackController::Structure(403);
            }

        }

    }
            

  

















   
}